/**
 * 
 */
package code.google.qinglan.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wxq
 * 
 */
public class FileHelper {

	private List<String> fileList;

	private String encode;

	/**
	 * 
	 */
	public FileHelper() {
		this.fileList = new ArrayList<String>();
	}

	/**
	 * 输出文件，编码为UTF-8 用记事本另存为：fileContent 全部为英文则为ansi 包含中文则为UTF-8
	 * 
	 * @param content
	 *            要输出的文件内容
	 * @param comspec
	 *            全路径名
	 */
	public void writeFile(String content, String comspec) {
		try {
			FileOutputStream fos = new FileOutputStream(comspec);
			Writer out = new OutputStreamWriter(fos, encode);
			out.write(content);
			out.close();
			fos.close();
		} catch (IOException e) {
			System.out.println("写文件操作出错！");
			e.printStackTrace();
		}
	}

	/**
	 * 读取文件内容
	 * 
	 * @param filePath
	 * @return String
	 */
	public String readFile(String filePath) {
		StringBuilder sb = new StringBuilder();
		try {
			File file = new File(filePath);
			System.out.println("File : " + filePath + "    Size :"
					+new FileInputStream( file).available() + "Bytes    loaded!");
			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file), encode);
			BufferedReader reader = new BufferedReader(read);
			String s = reader.readLine();
			while (s != null) {
				sb.append(s);
				sb.append("\r\n");
				s = reader.readLine();
			}
			reader.close();
		} catch (IOException e) {
			System.out.println("File : " + filePath + "  NotFound!");
		}
		return sb.toString();
	}

	/**
	 * 删除制定路径文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @return 是否删除成功
	 */
	public boolean deleteFile(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return file.delete();
		}
		return false;
	}

	/**
	 * @param path
	 *            文件路径
	 * @param suffix
	 *            后缀名
	 * @param isdepth
	 *            是否遍历子目录
	 * @return fileList
	 */
	public List<String> addListFiles(String path, String suffix, boolean isdepth) {
		path = FixPath.fixPath(path);
		File file = new File(path);
		List<String> search =   listFile(path, file, suffix, isdepth);
		fileList.addAll(search);
		return search;
	}

	/**
	 * 获取当前目录下文件路径
	 * 
	 * @param path
	 * @param f
	 * @param suffix
	 * @param isdepth
	 * @return
	 */
	private List<String> listFile(String path, File f, final String suffix,
			boolean isdepth) {
		// 是目录，同时需要遍历子目录
		List<String> resultList = new ArrayList<String>();
		if (!path.endsWith("/")) {
			path += "/";
		}
		String nowPath = path;
		String[] fl = f.list();
		List<File> directoryFile = new ArrayList<File>();
		List<String> directoryPath = new ArrayList<String>();
		for (int i = 0; i < fl.length; i++) {
			File ftemp = new File(f.getPath(), fl[i]);
			// 如果文件是目录，则继续调用方法。
			// System.out.println(ftemp.isDirectory());
			if (ftemp.isDirectory() && isdepth == true
					|| nowPath.equals(f.getAbsolutePath())) {
				directoryFile.add(ftemp);
				String tPath = nowPath + fl[i];
				directoryPath.add(tPath);
			} else {
				if (fl[i].endsWith("." + suffix)) {
					resultList.add(path + fl[i]);
				}
			}
		}
		for (int i = 0; i < directoryFile.size(); i++) {
			resultList.addAll(listFile(directoryPath.get(i),
					directoryFile.get(i), suffix, isdepth));
		}
		return resultList;
	}

	public List<String> getFileList() {
		return fileList;
	}

	public void setFileList(List<String> fileList) {
		this.fileList = fileList;
	}

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

}
